package com.jk.config;


import com.jk.bean.Staff;
import com.jk.service.ShorService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

import javax.annotation.Resource;
import java.util.List;

public class MyRealm extends AuthorizingRealm {

    @Resource
    private ShorService shorService;
    /**
     * 授权器
     * @param principals
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        String userBean =  principals.getPrimaryPrincipal().toString();
        List<String> list = shorService.getPermission(userBean);
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        info.addStringPermissions(list);
        return info;
    }

    /**
     * 认证器
     * @param token
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String username = (String) token.getPrincipal();
        Staff user = shorService.getUser(username);
        if (user.getStaffPwd()==null){

            return null;
        }
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(username,user.getStaffPwd(),this.getName());

        return info;
    }

}
